#include "Stdafx.h"
#include "Helper.h"

using namespace System;
using namespace System::Text;
using namespace System::Runtime::InteropServices;

Helper::Helper(void)
{
}
/*
String^ Helper::ToString(smiOCTETS value)
{
	if(value.len == 0 || value.ptr == NULL)
	{
		return String::Empty;
	}

	String^ returnValue = System::Runtime::InteropServices::Marshal::PtrToStringUni((IntPtr)value.ptr);
	return returnValue->Substring(0, returnValue->Length-1);
	/*
	unsigned long  J;
	BOOL  IsDisplayString = TRUE;
	for ( J=0; J < value.len && IsDisplayString; J++ )
	{  
	IsDisplayString = isprint( value.ptr[J] );
	} 
	System::Text::StringBuilder^ sbTemp = gcnew System::Text::StringBuilder();
	for(unsigned int i = 0; i < value.len; i++)
	{
	if(IsDisplayString)
	{			
	System::Char chrValue = (System::Char)value.ptr[i];
	sbTemp->Append(chrValue);
	//sbTemp->Append(".");
	}
	else
	{
	sbTemp->Append(String::Format("<0x{0:x2}>", value.ptr[i])); 
	}
	}
	return sbTemp->ToString();
	*/
//}*/

String^ Helper::ToString(smiOCTETS value)
{
	if(value.len == 0 || value.ptr == NULL)
	{
		return String::Empty;
	}

	array<unsigned char>^ arrayChar = gcnew array<unsigned char>(value.len);
	for(unsigned int i = 0; i < value.len; i++)
	{
		arrayChar[i] = value.ptr[i];
	}
	return SNMPManager::m_Encoding->GetString(arrayChar);
}

String^ Helper::ToString(smiOID value)
{
	if(value.len == 0 || value.ptr == NULL)
	{
		return String::Empty;
	}

	System::Text::StringBuilder^ sbTemp = gcnew System::Text::StringBuilder();
	for(unsigned int i = 0; i < value.len; i++)
	{
		//System::Char chrValue = (System::Char)outValue.ptr[i];
		sbTemp->Append(value.ptr[i].ToString());
		sbTemp->Append(".");
	}
	if(sbTemp->Length > 1)
	{
		sbTemp->Remove(sbTemp->Length - 1, 1);
	}
	return sbTemp->ToString();
}

String^ Helper::ToIpAddress(smiOCTETS value)
{
	if(value.len == 0 || value.ptr == NULL)
	{
		return String::Empty;
	}

	System::Text::StringBuilder^ sbTemp = gcnew System::Text::StringBuilder();
	for(unsigned int i = 0; i < value.len; i++)
	{
		//System::Char chrValue = (System::Char)value.ptr[i];
		sbTemp->Append(value.ptr[i].ToString());
		sbTemp->Append(".");
		//sbTemp->Append(".");
	}
	if(sbTemp->Length > 1)
	{
		sbTemp->Remove(sbTemp->Length - 1, 1);
	}
	return sbTemp->ToString();
}

smiOID Helper::ToOID(String^ oid)
{
	/*
	if(String::IsNullOrEmpty(oid))
	{
	smiOID retValue;
	retValue.len = 0;
	retValue.ptr = 0;
	return retValue;
	}
	*/
	LPCSTR lpOid = (LPCSTR)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(oid);
	//LPCSTR lpOid = (LPCSTR)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalUni(oid);
	smiOID retValue;
	SnmpStrToOid(lpOid, &retValue);
	System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)(void*)lpOid);
	return retValue;
}

/*
smiOCTETS Helper::ToOctets(String^ value)
{
	unsigned char* pValue = (unsigned char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalUni(value);
	//unsigned char* pValue = (unsigned char*)(void*)System::Runtime::InteropServices::Marshal::StringToHGlobalUni(value);
	smiOCTETS retValue;
	System::Text::UnicodeEncoding^ unicode = gcnew System::Text::UnicodeEncoding();
	retValue.len = unicode->GetByteCount(value);
	retValue.ptr = pValue;
	//System::Runtime::InteropServices::Marshal::FreeHGlobal((System::IntPtr)pValue);
	return retValue;
}
*/

smiOCTETS Helper::ToOctets(String^ value)
{
	if(String::IsNullOrEmpty(value))
	{
		smiOCTETS retValue;
		retValue.len = 0;
		retValue.ptr = NULL;
		return retValue;
	}

	array<unsigned char>^ tempBuffer = SNMPManager::m_Encoding->GetBytes(value);
	int size = Marshal::SizeOf(tempBuffer[0]) * tempBuffer->Length;
	IntPtr pnt = Marshal::AllocHGlobal(size);

	try
	{
		// Copy the array to unmanaged memory.
		Marshal::Copy(tempBuffer, 0, pnt, tempBuffer->Length);
	}
	finally
	{
		// Free the unmanaged memory.
		//Marshal::FreeHGlobal(pnt);
	}

	smiOCTETS retValue;
	retValue.len = tempBuffer->Length;
	retValue.ptr = (smiLPBYTE)pnt.ToPointer();
	return retValue;
}

String^ Helper::UnicodeToASCII(String^ unicodeString)
{
	//String^ unicodeString = "This string contains the unicode character Pi (\u03a0)";

	// Create two different encodings.
	Encoding^ ascii = Encoding::ASCII;
	Encoding^ unicode = Encoding::Unicode;

	// Convert the string into a byte array.
	array<Byte>^unicodeBytes = unicode->GetBytes( unicodeString );

	// Perform the conversion from one encoding to the other.
	array<Byte>^asciiBytes = Encoding::Convert( unicode, ascii, unicodeBytes );

	// Convert the new Byte into[] a char and[] then into a string.
	array<Char>^asciiChars = gcnew array<Char>(ascii->GetCharCount( asciiBytes, 0, asciiBytes->Length ));
	ascii->GetChars( asciiBytes, 0, asciiBytes->Length, asciiChars, 0 );
	String^ asciiString = gcnew String( asciiChars );

	// Display the strings created before and after the conversion.
	//Console::WriteLine( "Original String*: {0}", unicodeString );
	//Console::WriteLine( "Ascii converted String*: {0}", asciiString );
	return asciiString;
}
